package com.lbs.jiepiaisi.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.bean.NewsEntity;
import com.lbs.jiepiaisi.utils.GlideUtil;

import java.util.List;

/**
 *
 * 时间：2018/1/12 12:02
 * 作用：新闻列表
 */

public class NewsListAdapter extends BaseAdapter {

    List<NewsEntity.Result.Data> listItem;

    private Context context;



    public NewsListAdapter(Context context, List<NewsEntity.Result.Data> listItem) {
        this.context = context;
        this.listItem = listItem;
    }

    @Override
    public int getCount() {
        return listItem.size();
    }

    @Override
    public Object getItem(int position) {
        return listItem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh=null;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.list_item_newslist, null);
            vh=new ViewHolder();//一个老师座位表(座位上放数据)
            vh.rel_one = (RelativeLayout)convertView.findViewById(R.id.rel_one);
            vh.tv_one_title = (TextView) convertView.findViewById(R.id.tv_one_title);
            vh.tv_one_from = (TextView) convertView.findViewById(R.id.tv_one_from);
            vh.iv_one_logo = (ImageView) convertView.findViewById(R.id.iv_one_logo);

            vh.lin_three = (LinearLayout) convertView.findViewById(R.id.lin_three);
            vh.tv_three_title = (TextView) convertView.findViewById(R.id.tv_three_title);
            vh.tv_three_from = (TextView) convertView.findViewById(R.id.tv_three_from);
            vh.iv_three_logo1 = (ImageView) convertView.findViewById(R.id.iv_three_logo1);
            vh.iv_three_logo2 = (ImageView)convertView.findViewById(R.id.iv_three_logo2);
            vh.iv_three_logo3 = (ImageView)convertView.findViewById(R.id.iv_three_logo3);

            convertView.setTag(vh);
        }else{
            vh=(ViewHolder)convertView.getTag();
        }
        NewsEntity.Result.Data data = listItem.get(position);
        if(data.getThumbnail_pic_s02() == null && data.getThumbnail_pic_s03() == null){
            vh.lin_three.setVisibility(View.GONE);
            vh.rel_one.setVisibility(View.VISIBLE);

            vh.tv_one_title.setText(data.getTitle());
            vh.tv_one_from.setText("来源："+data.getAuthor_name() + "   "+data.getDate());
            GlideUtil.loadFromNet(context,data.getThumbnail_pic_s(),vh.iv_one_logo);
        }else {
            vh.lin_three.setVisibility(View.VISIBLE);
            vh.rel_one.setVisibility(View.GONE);


            vh.tv_three_title.setText(data.getTitle());
            vh.tv_three_from.setText("来源："+data.getAuthor_name() + "   "+data.getDate());
            GlideUtil.loadFromNet(context,data.getThumbnail_pic_s(),vh.iv_three_logo1);
            GlideUtil.loadFromNet(context,data.getThumbnail_pic_s02(),vh.iv_three_logo2);
            GlideUtil.loadFromNet(context,data.getThumbnail_pic_s03(),vh.iv_three_logo3);
        }
        return convertView;
    }

    class ViewHolder{
        RelativeLayout rel_one;
        TextView tv_one_title;
        TextView tv_one_from;
        ImageView iv_one_logo;

        LinearLayout lin_three;
        TextView tv_three_title;
        TextView tv_three_from;
        ImageView iv_three_logo1;
        ImageView iv_three_logo2;
        ImageView iv_three_logo3;

    }
}
